

function say() {
    alert("LC:290. 单词规律");
}
say();

/**
 * @param {string} pattern
 * @param {string} s
 * @return {boolean}
 */
var wordPattern = function(pattern, s) {
    //5:01-16
    //key a, b val:s元素

    var sArr = s.split(' ')

    if(sArr.length == 1 && pattern.length > 1){
        return false
    }

    var hashMap = {}
    var wordDic = {}

    for(var i = 0; i < sArr.length; i++){
        var key = pattern.charAt(i)

        if(typeof(hashMap[key]) == 'undefined'){
            hashMap[key] = sArr[i]
        }else if(hashMap[key] != sArr[i]){
            return false
        }

        var wordKey = sArr[i]
        if(typeof(wordDic[wordKey]) == 'undefined'){
            wordDic[wordKey] = key
        }else if(wordDic[wordKey] != key){
            return false
        }
    }

    return true
};